package easy;

public class AddBinary {
    //错误，无法处理大于整形的数字相加
    public String addBinary0(String a, String b) {
        int numberA = Integer.valueOf(a,2);
        int numberB = Integer.valueOf(b,2);
        return Integer.toBinaryString(numberA+numberB);
    }

    public String addBinary(String a,String b){
        int lengthA = a.length();
        int lengthB = b.length();
        int carry = 0,i = 0;
        String res = "";
        while((i<lengthA) || (i<lengthB) || (carry!=0)){
            int x = (i<lengthA)?Character.getNumericValue(a.charAt(lengthA-i-1)):0;
            int y = (i<lengthB)?Character.getNumericValue(b.charAt(lengthB-i-1)):0;
            res = (x+y+carry)%2 +res;
            carry = (x+y+carry)/2;
            i++;
        }
        return res;
    }
}

